<?php

class OrderController extends Controller
{

    /*
    * 获取列表分页
    *
    */
    public function actionGetListPaging()
    {

        try {
            //参数验证

            //检测
            $user_id = SessionService::getUserIDBySessionid();


            $searchsql = Util::initSearchSql();
            $searchsql = Util::getSerarchStr($searchsql,"order_id","order_id");
            $searchsql = Util::getSerarchStr($searchsql,"order_no","order_no");
            $searchsql = Util::getSerarchStr($searchsql,"status","status");
            $searchsql = Util::getSerarchStr($searchsql,"type_id","type_id");
            $searchsql = Util::getSerarchStr($searchsql,"worker_id","worker_id");
            $searchsql = Util::getSerarchStr($searchsql,"user_id","user_id");





            //获取count
            $totalcount = OrderList ::model()->count($searchsql);


                //排序
                $searchsql = $searchsql . " order by create_time desc";

                //加上分页控制
                $searchsql = Util::addLimitSql($searchsql);

                //获取list
                $list = OrderList ::model()->findAll($searchsql);


                Util::responseList($totalcount, $list);

            } catch (Exception $e) {
            Util::responseErrorMsg($e->getMessage());

        }


    }

    /*
 * 用户端
* 取消接单
*
*/
    public function actionCancelOrder()
    {

        try {
            //检测
            $user_id = SessionService::getUserIDBySessionid();

            //参数验证
            $order_id = Util::getParamNotEmpty("order_id");



            //验证存在
            $order = OrderList::model()->findByPK($order_id);

            Util::checkExistAndNotDelete($order);

//            //工人验证
//            if($order->user_id != $user_id)
//                throw new Exception("不能处理别人的订单");

            //检查状态

            if($order->status != MsgConst::ORDER_STATUS_WAITING_ACCEPTED)
                throw new Exception("该订单已被处理");



            //拒绝订单
            OrderService::cancelOrder($order,"用户取消订单");


            //返回数据
            Util::responseMsg(MsgConst::RETURN_SUCCESS);


        } catch (Exception $e) {
            Util::responseErrorMsg($e->getMessage());

        }
    }

    /*
* 用户端
*  开始订单
*
*/
    public function actionstartOrder()
    {

        try {
            //检测
            $user_id = SessionService::getUserIDBySessionid();

            //参数验证
            $order_id = Util::getParamNotEmpty("order_id");

            //验证存在
            $order = OrderList::model()->findByPK($order_id);

            Util::checkExistAndNotDelete($order);

//            //工人验证
//            if($order->user_id != $user_id)
//                throw new Exception("不能处理别人的订单");

            //检查状态

            if($order->status != MsgConst::ORDER_STATUS_CONFIRMED)
                throw new Exception("该订单已被处理");



            //支付订单
            OrderService::startOrder($order);


            //返回数据
            Util::responseMsg(MsgConst::RETURN_SUCCESS);


        } catch (Exception $e) {
            Util::responseErrorMsg($e->getMessage());

        }
    }
}